package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Admin;
import com.pojo.Contents;
import com.pojo.Dictionary;
import com.service.ContentsService;
import com.service.DictionaryService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/appshouye")
public class AppShouyePeizhiAction {

    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ContentsService contentsService;

    @Value("${upload.dir}")
    private String upload;

    @Value("${upload.tmp.dir}")
    private String tmp;
    /**
     * 查询首页跑马灯配置
     */
    @RequestMapping("/select_paomadeng_peizhi")
    public ResponseBean select_dayinji_peizhi() {

        Dictionary paomadeng = dictionaryService.getOne(new QueryWrapper<Dictionary>().eq("code","paomadeng").eq("type","paomadeng").last("limit 1"));
        if(paomadeng != null){
            return new ResponseBean(0,1,paomadeng);
        }
        return new ResponseBean(1,1,"");
    }
    /**
     * 修改跑马灯配置
     * @param dictionary  跑马灯配置
     * @return
     */
    @RequestMapping("/addpaomadeng")
    public ResponseBean addpaomadeng(@RequestBody Dictionary dictionary) {

        if(StringUtil.isNull(dictionary.getCode())){
            dictionary.setCode("paomadeng");
        }
        if(StringUtil.isNull(dictionary.getType())){
            dictionary.setType("paomadeng");
        }

        if(StringUtil.isNull(dictionary.getId())){  //新增跑马灯配置
            if (dictionaryService.save(dictionary)) {
                return new ResponseBean(200, 1, "新增成功");
            }
        }else {  //修改跑马灯配置
            if (dictionaryService.updateById(dictionary)) {
                return new ResponseBean(200, 1, "修改成功");
            }
        }
        return new ResponseBean(500, 1, "操作失败");
    }

    /**
     * 分页查询app轮播
     * @param title  关键字
     * @param authorName  状态(0为禁用，1为启用)
     * @param page  页面
     * @param limit  条数
     * @return
     */
    @RequestMapping("/selectshouye_lunbo")
    @ResponseBody
    public ResponseBean selectshouye_lunbo(String title, @RequestParam(required = false) String authorName, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit){
        IPage iPage = contentsService.selectshouye_lunbo(page,limit,title,authorName);
        return new ResponseBean(0,iPage.getTotal(),"查询成功",iPage.getRecords());
    }

    /**
     * 新增app首页轮播
     * @param contents
     * @param request
     * @return
     */
    @RequestMapping("/saveLunbo")
    @ResponseBody
    public ResponseBean saveLunbo(@RequestBody Contents contents, HttpServletRequest request) {
        contents.setCreate_time(new Date());
        Admin admin = (Admin) request.getSession().getAttribute("admin");
        if(null != admin){
            contents.setAdmin_id(admin.getId());
        }

        String content = "<meta name = 'viewport' content = 'width = device-width,initial-scale = 1,minimum-scale = 1, maximum-scale = 1, user-scalable = no'/>\n" + contents.getContent();
        contents.setContent(content);
        if (contentsService.save(contents)) {
            if (StringUtil.isNotNull(contents.getImage())) {
                FileUtil.copy(tmp + contents.getImage(), upload, true) ;
                //tmp复制图片到upload
//                String srcPathStr = tmp + notice.getPicture(); //源文件地址
//                String desPathStr = upload; //目标文件地址
//                CopyFile.copy(srcPathStr, desPathStr);
            }
            return new ResponseBean(200, 1, "添加成功", "添加成功");
        }
        return new ResponseBean(500, "添加失败", "添加失败");
    }

    /**
     * 修改app轮播状态
     */
    @RequestMapping("/upStatus")
    public ResponseBean changeStatus(String id,Integer status) {
        Contents contents = new Contents();
        contents.setId(id);
        contents.setStatus(status);
        if(contentsService.updateById(contents)){
            contents = contentsService.getById(contents.getId());
            if(contents.getStatus() == 1){
                List<Contents> list = contentsService.list(new QueryWrapper<Contents>().ne("id",contents.getId()).eq("status",1));
                list.stream().forEach(p-> {
                    p.setStatus(0);
                });
                contentsService.updateBatchById(list);
            }
            return new ResponseBean(0,1,"修改状态成功");
        }
        return new ResponseBean(1,1,"修改状态失败");
    }
    /**
     * 根据ID查询app首页轮播
     */
    @RequestMapping("/getContentsByid")
    public ResponseBean getContentsByid(String id) {
        Contents contents = contentsService.getById(id);
        return new ResponseBean(0,1,"成功",contents);
    }
    /**
     * 修改app首页轮播
     */
    @RequestMapping("/updShouye_lunbo")
    public ResponseBean updShouye_lunbo(@RequestBody Contents contents) {
        Contents queryNotice = contentsService.getById(contents.getId());
        if(contentsService.updateById(contents)){
            if(contents.getStatus() == 1){
                List<Contents> list = contentsService.list(new QueryWrapper<Contents>().ne("id",contents.getId()).eq("status",1));
                list.stream().forEach(p-> {
                    p.setStatus(0);
                });
                contentsService.updateBatchById(list);
            }
            if(StringUtil.isNotNull(contents.getImage()) && contents.getImage().equals(queryNotice.getImage())==false){
                FileUtil.copy(tmp + contents.getImage(), upload, true) ;
                //tmp复制图片到upload
//                String srcPathStr = tmp + notice.getPicture(); //源文件地址
//                String desPathStr = upload; //目标文件地址
//                CopyFile.copy(srcPathStr, desPathStr);
            }
            return new ResponseBean(3,1, "修改成功");
        }
        return new ResponseBean(1,1,"修改失败");
    }
}
